//
//  MBProgressHUD+HUD.m
//  LFCF
//
//  Created by 邓伟浩 on 2019/4/17.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "MBProgressHUD+HUD.h"

#import "UIImage+GIF.h"

#define WHDelayTime 1.5

@implementation MBProgressHUD (HUD)

/** 获取HUD **/
+ (MBProgressHUD *)HUD {
    return [MBProgressHUD HUDForView:[UIApplication sharedApplication].delegate.window];
}

+ (MBProgressHUD *)makeCommonHudForMsg:(NSString *)message inView:(UIView *)view forMode:(MBProgressHUDMode)mode {
    view = view?:[UIApplication sharedApplication].delegate.window;
    [MBProgressHUD hideHUDForView:view];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.color = [UIColor blackColor];
    hud.contentColor = [UIColor whiteColor];
    hud.mode = mode;
    
    if (message) {
        hud.detailsLabel.text = message;
        hud.detailsLabel.textColor = [UIColor whiteColor];
        hud.detailsLabel.font = FONT(10);
    }
    
    return hud;
}

/** 只显示菊花 **/
+ (void)showLoadHUD {
    [self showLoadHUDInView:nil];
}

+ (void)showLoadHUDInView:(UIView *)view {
    [self makeCommonHudForMsg:nil inView:view forMode:MBProgressHUDModeIndeterminate];
}

/** 显示菊花+文字 **/
+ (void)showLoadHUDMsg:(NSString *)message {
    [self showLoadHUDMsg:message inView:nil];
}

+ (void)showLoadHUDMsg:(NSString *)message inView:(UIView *)view {
    [self makeCommonHudForMsg:message inView:view forMode:MBProgressHUDModeIndeterminate];
}

/** 显示文字-->几秒钟后消失 **/
+ (void)showHUDMsg:(NSString *)message {
    [MBProgressHUD showHUDMsg:message inView:nil];
}

+ (void)showHUDMsg:(NSString *)message inView:(UIView *)view {
    [MBProgressHUD showHUDMsg:message inView:view duration:WHDelayTime];
}

+ (void)showHUDMsg:(NSString *)message inView:(UIView *)view duration:(NSTimeInterval)duration {
    MBProgressHUD *hud = [MBProgressHUD makeCommonHudForMsg:message inView:view forMode:MBProgressHUDModeText];
    [hud hideAnimated:YES afterDelay:duration];
}

/** 环形进度条+文字 **/
+ (void)showCircularHUDProgress {
    [MBProgressHUD showCircularHUDProgressMessage:@"下载中..." inView:nil];
}

+ (void)showCircularHUDProgressMessage:(NSString *)message inView:(UIView *)view {
    [MBProgressHUD makeCommonHudForMsg:message inView:view forMode:MBProgressHUDModeAnnularDeterminate];
}

/** 水平进度条 + 文字 **/
+ (void)showBarHUDProgress {
    [MBProgressHUD showBarHUDProgressMessage:@"加载中..." inView:nil];
}

+ (void)showBarHUDProgressMessage:(NSString *)message inView:(UIView *)view {
    [MBProgressHUD makeCommonHudForMsg:message inView:view forMode:MBProgressHUDModeDeterminateHorizontalBar];
}

/** 自定义图片 + 文字 **/
+ (void)showCustomViewHUD:(NSString *)msg imageName:(NSString *)imageName {
    [MBProgressHUD showCustomViewHUD:msg imageName:imageName inView:nil duration:WHDelayTime];
}

+ (void)showCustomViewHUD:(NSString *)msg imageName:(NSString *)imageName inView:(UIView *)view duration:(NSTimeInterval)duration {
    MBProgressHUD *hud = [MBProgressHUD makeCommonHudForMsg:msg inView:view forMode:MBProgressHUDModeCustomView];
    // 设置显示的图片
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.square = YES;
    [hud hideAnimated:YES afterDelay:duration];
}


/** 自定义加载动画 + 文字 **/
+ (void)showCustomGifHUD:(NSString *)msg imageName:(NSString *)imageName {
    [MBProgressHUD showCustomGifHUD:msg imageName:imageName inView:nil];
}

+ (void)showCustomGifHUD:(NSString *)msg imageName:(NSString *)imageName inView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD makeCommonHudForMsg:msg inView:view forMode:MBProgressHUDModeCustomView];
    
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.square = YES;
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
    rotationAnimation.duration = 0.6;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 20000;//重复次数
    [hud.customView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}


/** 隐藏HUD */
+ (void)hideHUD {
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view {
    view = view?:[UIApplication sharedApplication].delegate.window;
    [MBProgressHUD hideHUDForView:view animated:YES];
}

@end
